package ui.fragment.PosControl;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import base.LazyFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import coom.MyType;
import event.CloseActEvent;
import event.HasPushEvent;
import event.NetErrorEvent;
import ui.activity.poscontrol.adapter.PushBackRecordMyAdapter;
import ui.activity.poscontrol.beanmodel.MyPosBean;
import ui.activity.poscontrol.beanmodel.PosInfoBean;
import ui.activity.poscontrol.beanmodel.PushBackPeopleBean;
import ui.activity.poscontrol.beanmodel.PushBackPosBean;
import ui.activity.poscontrol.biz.MyPosControlBiz;
import ui.activity.poscontrol.component.DaggerMyPosControlComponent;
import ui.activity.poscontrol.contract.MyPosControlContract;
import ui.activity.poscontrol.module.MyPosControlModule;
import ui.activity.poscontrol.presenter.MyPosControlPresenter;

/**
 * Created by fangjunsong on 2019年3月19日17:03:30.
 * <p>
 * 审核中
 */

public class PushBackRecordMyFra extends LazyFragment implements MyPosControlContract.View {


    @Inject
    MyPosControlPresenter presenter;
    @Inject
    MyPosControlBiz biz;

    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    @BindView(R.id.pushpeople_list)
    RecyclerView rv;
    @BindView(R.id.posrecord_txt_total)//终端数量txt
            TextView posrecord_txt_total;

    List<PushBackPeopleBean> databeans = new ArrayList<>();
    PushBackRecordMyAdapter adp;
    private boolean isPrepared;// 界面否
    private boolean isALeady = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.activity_pushbackrecordmy, container, false);
        ButterKnife.bind(this, view);
        DaggerMyPosControlComponent.builder().myPosControlModule(new MyPosControlModule(this)).build().inject(this);
        presenter.setBiz(biz);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setItemAnimator(new DefaultItemAnimator());

        isPrepared = true;
        getData();

        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData();
            }
        });
        return view;
    }

    @Override
    protected void lazyLoad() {
//        if (isPrepared && isVisible && isALeady) {
//            // TODO 在这里处理逻辑
//            isALeady = false;
//        }
    }

    private void getData(){
        databeans.clear();
        biz.queryPushBackRecordPeopleData(TobuyApplication.getCustomId(),1,new BaseBiz.Callback<List<PushBackPeopleBean>>() {
            @Override
            public void onSuccess(List<PushBackPeopleBean> pushBackPeopleBean) {

                ptr.onRefreshComplete();
                int totalCount =0;
                for (int i = 0;i<pushBackPeopleBean.size();i++){
                    totalCount+= pushBackPeopleBean.get(i).getCount();
                }

                posrecord_txt_total.setText("终端数量："+totalCount);
                databeans.addAll(pushBackPeopleBean);
//                if(null == adp){
                    adp = new PushBackRecordMyAdapter(getActivity(),databeans);
                    rv.setAdapter(adp);
//                }else{
//                    adp.notifyDataSetChanged();
//                }
            }

            @Override
            public void onFailure(List<PushBackPeopleBean> pushBackPeopleBean) {
                ptr.onRefreshComplete();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerCancelOrder(HasPushEvent event) {
        biz.queryPushBackRecordPosData(TobuyApplication.getCustomId(),event.toId,event.descs,event.ascs,1,new BaseBiz.Callback<PushBackPosBean>() {
            @Override
            public void onSuccess(PushBackPosBean pushPosBean) {
                for ( int i = 0;i<databeans.size();i++){
                    if(databeans.get(i).getToId() == event.toId) {
                        databeans.get(i).getPosRecords().clear();
                        databeans.get(i).getPosRecords().addAll(pushPosBean.getRecords());
                        break;
                    }
                }
                adp.setData(databeans);

            }

            @Override
            public void onFailure(PushBackPosBean pushPosBean) {

            }

        });
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCloseActEvent(CloseActEvent event) {
        if (event.getType() == MyType.CompleteOrder) {
        }
    }


    @Override
    public void loadMoreComplete() {

    }

    @Override
    public void hasLoadMore(boolean b) {

    }

    @Override
    public void refreshComplete() {

    }

    @Override
    public void refreshData() {

    }

    @Override
    public void upDateUI(MyPosBean infoBean, List<MyPosBean.RecordsBean> bean, boolean refresh) {

    }

    @Override
    public void upDateInfoListUI(PosInfoBean infoBean, List<PosInfoBean.RecordsBean> bean) {

    }
}
